// pages/Apply_ordering/Apply_ordering.js
import api from '../../Sendingmethod/Sendingmethod'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        leftarr: [
        ],
        letindex: 0,
        rghitindex: 0,
        goodstypelist: [],
        letid: "",
        rghitid: '',
        shopist: [],
        keyword: "",
        /**选择记录存储 */
        shopjilu: [
        ]
    },
    /**
     * 
     * 步进器增加
     */
    addshop(e) {
        console.log('增加', e);
        console.log(this.data.shopjilu.length);
        //进行赋值
        const index1 = this.data.shopist.findIndex((item) => item.id == e.detail.id)
        if (index1 !== -1) {
            this.data.shopist[index1].num = e.detail.num
        }
        this.setData({
            shopist: this.data.shopist
        })


        if (this.data.shopjilu.length <= 0) {
            //第一次选择 直接加入
            this.data.shopjilu.push(e.detail)
        } else {
            //后面选择 先查找 或者更新

            const index = this.data.shopjilu.findIndex(item => item.id == e.detail.id); // 输出: 1
            if (index !== -1) {
                console.log('索引查找成功', index);
                this.data.shopjilu[index].num = e.detail.num
            } else {
                console.log('没有查询到');
                this.data.shopjilu.push(e.detail)
            }
            console.log(this.data.shopjilu, '更新后记录数组');
            this.setData({
                shopjilu: this.data.shopjilu
            })

        }
    },

    navigate() {
        let data = JSON.stringify(this.data.shopjilu)
        let num = this.data.shopjilu.filter((item, index) => item.num > 0)
        console.log(data, num, '---------------------------------');
        if (num.length> 0) {
            wx.navigateTo({
                url: `/pages/submit_order/submit_order?shopslist=${data}`,
            })
        }else{
            wx.showToast({
              title: '暂未选择商品',
              icon:"error",
              duration:2000
            })
        }

    },
    /**
     * 侧边栏选择
     */
    left_select(e) {
        console.log(e);
        console.log(e.target.dataset.index);
        this.setData({
            letindex: e.target.dataset.index,
            letid: e.target.dataset.id,
            rghitindex: 0,
            rghitid: this.data.goodstypelist[e.target.dataset.index].childlist[0].id
        })
        this.getmygoodsList()
    },
    /**e
     * 右边侧边栏选择
     */
    righttap(e) {
        console.log(e);

        this.setData({
            rghitindex: e.target.dataset.index,
            rghitid: e.target.dataset.id,
        })
        this.getmygoodsList()
    },
    /**
     * 
     *滚动事件
     */
    scroll(e) {
        if (!this.data.letblen) {
            console.log('左侧触发滚动');
        } else {
            console.log(e, '右侧触发滚动');
        }
        this.setData({
            letblen: true
        })

    },
    /**
     * 获取订货分类
     * 
     */
    async getfenle() {
        let res = await api.mygoodsType()
        console.log(res, '订货分类');
        if (res.code == 0) {
            this.setData({
                goodstypelist: res.data,
                letid: res.data[0].id,
                rghitid: res.data[0].childlist[0].id
            })
            this.getmygoodsList()
        }
    },
    /**
     * 
     * 获取商品列表
     */
    async getmygoodsList() {
        console.log({
            type: this.data.letid,
            subtype: this.data.rghitid,
            keyword: this.data.keyword
        }, '传输数据');
        let res = await api.mygoodsList({
            type: this.data.letid,
            subtype: this.data.rghitid,
            keyword: this.data.keyword
        })
        console.log(res, '商品列表');
        if (res.code == 0) {
            this.setData({
                shopist: res.data ? this.liebiaochuli(res.data) : []
            })
            console.log(this.data.shopist, '处理后的数组');
        } else {

        }
    },
    /**
     * 商品列表处理
     */
    liebiaochuli(data) {

        let newdata = data.map((item, index) => {
            return {
                ...item,
                num: 0
            }
        })

        if (this.data.shopjilu.length > 0) {
            // 有数量数组 进行赋值
            console.log('有数量数组 进行赋值');
            // newdata.forEach((item, index)=>{
            //   let newindex= item.childlist.findIndex((item1)=> item1.id=)
            // })
            this.data.shopjilu.forEach((item) => {
                let newindex = newdata.findIndex((item1) => item1.id == item.id)
                if (newindex !== -1) {
                    newdata[newindex].num = item.num
                }
            })
        }
        console.log(newdata, '添加num处理后的数组');
        return newdata

    },
    handleConfirm() {
        console.log('键盘确认事件');
        this.getmygoodsList()
    },
    /**
     * 输入事件
     */
    inpubox(e) {
        console.log(e.detail.value);
        this.setData({
            keyword: e.detail.value
        })
        if (e.detail.value == "") {
            this.getmygoodsList()
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getfenle()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})